//index.js
var cf = require("../../config.js");
var util = require("../../utils/util.js");
var cusmallToken = wx.getStorageSync('cusmallToken');
var searchHandle = require("../template/searchHandle.js");
var categoryTabHandle = require("../template/categoryTabWidget.js");
var baseHandle = require("../template/baseHandle.js");
var commHandle = require("../template/commHandle.js");
//获取应用实例
var app = getApp();
Page(Object.assign({}, baseHandle, commHandle, searchHandle, categoryTabHandle, {

  /**
   * 页面的初始数据
   */
  data: {
    decoration: {},
    shoppingCartCount: 0,
    app: app,
    id: "",
    extConfig: wx.getExtConfigSync ? wx.getExtConfigSync() : {},
    bannerHeight: {},
    staticResPath: cf.config.staticResPath,
    userImagePath: cf.config.userImagePath,
    showZan: false,
    isThumbs: false,//是否点过赞
    playBgMusic: true,
    likeNum: 0,
    mallSiteId: "",

    multInfo: {},
    multInfoArr: {},
    multInfoAddr: "",
    multClassArr: {},
    haveMutl: false,
    haveSearch: false,

    communityHandleData: {
      topicList: [],
      categoryList: []
    },
    topicArr: {},
    categoryArr: {},
    adInfo: { show: false }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showShareMenu({
      withShareTicket: false
    });
    //生命周期函数--监听页面加载
    var ctx = this;
    app.getUserInfo(this, options, function (userInfo, res) {

      var pageId = options.pageId;
      if (options.scene) {
        // 处理预览ID
        var scene = decodeURIComponent(options.scene);
        var params = scene.split("=");
        if (params[0] == "pageid") {
          pageId = params[1];
        }
      }
      cusmallToken = wx.getStorageSync('cusmallToken');
      ctx.fetchData();
      ctx.setData({
        eId: options.eId || wx.getStorageSync("eId") || "",
        companyInfo: wx.getStorageSync('companyInfo'),
        openType: options.openType || ""
      });
      wx.setStorageSync("eId", ctx.data.eId);//刷新 缓存的员工id
      if (undefined !== options.source && ctx.data.eId) {//用户绑定card关系
        util.addRelation(ctx.data.eId, cusmallToken, options.source);
      }
      util.addBehaviorTrail(ctx.data.eId, 7);
      //5 官网页底部广告
      ctx.getAdInfo(wx.getStorageSync('advertConfig'), 5);
    });
  },
  search: function (e) {
    wx.navigateTo({
      url: '/pages/search/search?keyword=' + e.detail.value,
    })
  },
  loadNavPanelPageContent: function () {
    // 微页面里面不能使用标签导航
  },
  fetchData: function (hideLoading) {
    var that = this;
    if (!hideLoading) {
      wx.showLoading({
        title: '加载中',
      });
    }
    wx.request({
      url: cf.config.pageDomain + '/applet/mobile/visitcard/decorate/getVisitCardDecorate',
      data: {
        cusmallToken: cusmallToken,
        // pageId: that.data.id
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var decorationData = {};
        decorationData = JSON.parse(res.data.model.decorate.brandDecorate || "{}");

        var headerData = decorationData.header_data;
        wx.setNavigationBarTitle({
          title: headerData.title || "公司官网"
        })
        util.processDecorationData(decorationData, that);
        for (var i in decorationData.items) {
          var item = decorationData.items[i];
          let titleName = encodeURIComponent(headerData.title);


        }
        that.setData({
          decoration: decorationData,
        });
        if (that.data.bgMusic) {
          that.audioCtx = wx.createAudioContext('bgMusic');
          that.audioCtx.play();
        }
        if (that.data.haveSearch) {//获取当前地址
          let callback = function (data) {
            that.setData({
              multInfoAddr: data.model.address
            });
          }
          util.autoGeyAddr(callback, cusmallToken);
        }
        wx.hideLoading();
      },
      complete(){
        wx.stopPullDownRefresh();
      }
    })
  },

  navgateTap: function () {

  },
  changeRoute: function (url) {
    wx.navigateTo({
      url: `../${url}/${url}`
    })
  },
  navTabFuc: function (e) {
    console.log(e);
    var url = e.currentTarget.dataset.url;
    if (!url) {
      return false;
    }
    if (url.indexOf("tel:") == 0) {
      wx.makePhoneCall({
        phoneNumber: url.split(":")[1]
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var ctx = this;
    ctx.fetchData();
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角
   */
  onShareAppMessage: function () {
    let ctx = this;
    let path = "/pages/company_index/company_index?eId=" + ctx.data.eId + "&source=1";
    let title;
    if (ctx.data.companyInfo) {
      title = ctx.data.companyInfo.shortName;
    }
    let shareObj = {
      title: title,
      path: path,
      success: function (res) {
        util.addBehaviorTrail(ctx.data.eId, 3);
        // 成功
      },
      fail: function (res) {
        // 失败
      }
    };

    return shareObj;
  }
}))